<?php
	session_start();
	include "db_function.php";
	if(isset($_POST['log'])){//登陆
		$isOK = login($_POST['username'], $_POST['pwd']);
		if($isOK !== false){//正常
			$_SESSION['uid'] = $isOK;
			$_SESSION['username'] = $_POST['username'];
			header('location:index.php');
		}else{
			echo "username or password is Invalid;";
		}
	}elseif(isset($_POST['reg'])){//注册
		$isOK = checkUsernameIsOK($_POST['username']);
		if(!$isOK){
			if($_POST['pwd'] != null){ 
				echo "username invalid,please try another one!<a href='register.php'>Try again<a/>";
				die;
			}else
			echo "Please input password";
			die;
		}
		$result = create_user($_POST['username'],$_POST['pwd']);
		if($result){
			$_SESSION['uid'] = $db->lastInsertId();
			$_SESSION['username'] = $_POST['username'];
			header("location:index.php");
		}
	}else{
		echo "Invalid Access";
	}
?>